$(function () {
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        webProList: null,
        iId: 0,
        params: {
            iSchoolId: null,
            iStatus: null,
            sTitle: null,
        },
        init: function () {
            this.webProList = new xhDataGrid($('#list'), 'webproblem/getWebProblem');
            this.webProList.parseToolbar().init();
            xhExtra.disableButton($('#bt_list_del'), 'eu');
            xhExtra.disableButton($('#bt_list_edit'), 'eu');
            this.funObj().bindDataGridEvent();
            this.listener();
        },
        listener: function () {
            var $this = this;
            var $length = 0;
            // 绑定搜索事件
            $('#bt_list_search').click(function () {
                $this.funObj().search();
            });
            $('#bt_list_add').click(function () {
                $this.funObj().addWebProblem();
            });
            //省级selectchange事件
            $('#school_province').combobox({
                onSelect: function(param){
                    // console.log($('#province').combobox('getText'));
                    var url = 'getCityDataByProvId?provId='+param.value;
                    $('#school_city').combobox('reload', url);
                    $('#school_city').combobox('setValue', '--市--');
                }
            });
            //市级
            $('#school_city').combobox({
                onSelect: function(param){
                    // console.log($('#province').combobox('getText'));
                    var url = 'getZoneDataByCityId?cityId='+param.value;
                    $('#school_zone').combobox('reload', url);
                    $('#school_zone').combobox('setValue', '--区--');
                }
            });
            $('#school_area').combotree({
                url: 'getDepData',
                animate:true,
                onLoadSuccess:function(node,data){
                    $length = data.length;
                    $.each(data,function (id,value) {
                        if (value.children !== undefined) {
                            $length = $length + value.children.length;
                        }
                    })
                },
                onShowPanel:function(){
                    if ($length <= 8){
                        $(this).combobox('panel').height("auto");//高度自适应
                    }else{
                        $(this).combobox('panel').height("200");//高度自定义
                    }
                },
                onBeforeSelect: function (node) {
                    var t = $(this).tree;
                    var isLeaf = t('isLeaf', node.target);
                    if (!isLeaf) {//选择的不是叶子节点
                        return false;
                    }
                }
            });

            // 绑定删除事件
            $('#bt_list_del').on('click', function () {
                $this.funObj().deleteWebProblem();
            });
            // 绑定编辑事件
            $('#bt_list_edit').on('click', function () {
                $this.funObj().editWebProblem();
            });

            //导出
            //导出excel
            $('#bt_list_export').click(function () {
                $this.funObj().exportWebProblem();
            });
            //
            $this.webProList.onDblClickRow = function(rowIndex, rowData){
                var problemId = rowData.iId;
                window.top.xhTop.createNewTab('网络问题解答', 'webproblem/webProblemAnswerList?iId=' + problemId, true);
                return false;
            };

        },
        funObj: function () {
            var $this = this;
            return {
                bindDataGridEvent: function () {
                    $this.webProList.onCheck = function (rowIndex, rowData) {
                        if ($this.iId == rowData['iId']) {
                            $this.iId = 0;
                            $this.webProList.command('unselectRow', rowIndex);
                            $('#bt_list_del').setButtonDisable('eu');//编辑enable
                            $('#bt_list_edit').setButtonDisable('eu');//编辑enable
                            return false;
                        } else {
                            $this.iId = rowData['iId'];
                            $('#bt_list_edit').setButtonEnable('eu');//编辑enable
                            $('#bt_list_del').setButtonEnable('eu');//删除enable
                        }
                    }
                },

                // 生成网络问题表单
                addWebProblem: function () {
                    window.top.xhTop.createNewTab('新增网络问题', 'webproblem/webProblemForm', true);
                },

                // 生成申请表单
                editWebProblem: function () {
                    var id = $this.iId;
                    if (!id) {
                        window.top.xhTop.showError('请选择要操作的问题！');
                        return false;
                    }
                    window.top.xhTop.createNewTab('编辑网络问题', 'webproblem/webProblemForm?iId='+ id, true);
                },

                // 查找
                search: function () {
                    var sSchoolName = xhExtra.getValue($('#list_search'));
                    var sStatus = xhExtra.getValue($('#list_status'));
                    var province = $('#school_province').combobox('getText');
                    var city = $('#school_city').combobox('getText');
                    var zone = $('#school_zone').combobox('getText');
                    var schoolArea = $('#school_area').combotree('getValue');
                    $this.webProList.reload({sSchoolName: sSchoolName, status: sStatus, province: province, city:city, zone:zone, schoolArea:schoolArea});
                },

                exportWebProblem: function() {
                    var queryParams = {};
                    queryParams.sSchoolName   = xhExtra.getValue($('#list_search'));
                    queryParams.status     = xhExtra.getValue($('#list_status'));
                    queryParams.province    = $('#school_province').combobox('getText');
                    queryParams.city       = $('#school_city').combobox('getText');
                    queryParams.zone       = $('#school_zone').combobox('getText');
                    queryParams.schoolArea   = $('#school_area').combotree('getValue');
                    location.href = baseUrl + 'webproblem/exportProblemData?'+ xhTool.parseParam(queryParams);
                },

                // 删除
                deleteWebProblem: function () {
                    if ($this.iId == 0) {
                        window.top.xhTop.showError('请选择要删除的问题!');
                        return false;
                    }
                    xhExtra.ajaxRequest('webproblem/delWebProblem', {id: $this.iId}, {check: true}, function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.webProList.reload();
                    });
                },
            }
        }
    }
};